package Demo67;

/**
 * 二进制求和
 * https://leetcode.cn/problems/JFETK5/description/
 */
class Solution {
    public String addBinary(String a, String b) {
        StringBuffer ret = new StringBuffer();
        int cur1 = a.length() - 1, cur2 = b.length() - 1,tmp = 0;
        while(cur1 >= 0 || cur2 >= 0 || tmp != 0) {
            if(cur1 >= 0) tmp += a.charAt(cur1--) - '0';
            if(cur2 >= 0) tmp += b.charAt(cur2--) - '0';
            ret.append((char)((char)(tmp % 2) + '0'));
            tmp /= 2;
        }
        ret.reverse();
        return ret.toString();
    }
}